package main.java.web.servlet;

import main.java.service.ResultService;
import main.java.service.impl.ResultServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updateResultServlet")
public class UpdateResultServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String resStr = request.getParameter("res");
        String eval = request.getParameter("eval");
        int res = Integer.parseInt(resStr);

        HttpSession session = request.getSession();
        String stuID = (String) session.getAttribute("stu_id");
        String taskID = (String) session.getAttribute("task_id");
        // service更新数据
        ResultService service = new ResultServiceImpl();
        service.update(Integer.parseInt(stuID), Integer.parseInt(taskID), res, eval);

        session.removeAttribute("stu_id");
        session.removeAttribute("task_id");

        request.getRequestDispatcher("/correctionServlet?task_id="+taskID).forward(request, response);
    }
}
